//
//  TutorialScene.m
//  iFly
//
//  Created by User-23 on 12/14/14.
//  Copyright (c) 2014 TeamDifferent. All rights reserved.
//

#import "TutorialScene.h"
#import "Constants.h"
#import "MainMenuScene.h"
#import "CharacterNode.h"
#import "Joystick.h"

@interface TutorialScene ()
@property NSString* previousScene;
@property SKSpriteNode* background;
@end

@implementation TutorialScene

-(instancetype) initWithSize:(CGSize)size andPreviousScene:(NSString*) previousScene {
    if (self = [super initWithSize:size]){
        SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"amazing clouds"]];

        background.position = CGPointZero;
        background.anchorPoint = CGPointZero;
        [self addChild:background];
        
        SKLabelNode* backLabel = [Constants createLabelWithFont:@"Chalkduster" text:@"Back To Main Menu" fontColor:[SKColor blueColor] fontSize:25];
        float backLabelMargin = self.frame.size.width * backMenuLabelScale;
        backLabel.position = CGPointMake(backLabel.frame.size.width / 2 + backLabelMargin, backLabelMargin);
        backLabel.name = backLabelName;
        [self addChild:backLabel];
        
        [self setupTutorial];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];
    
    SKTransition *transition = [SKTransition fadeWithDuration:0.4];
    
    if (node.name == backLabelName) {
        MainMenuScene* mainMenu = [MainMenuScene sceneWithSize:self.frame.size];
        [self.view presentScene:mainMenu transition:transition];
    }
}

-(void)setupTutorial {
    NSString* normalFont = @"Futura-CondensedExtraBold";
    NSInteger fontSize = self.frame.size.height*fontSizeOffset;
    float scaleCoef= self.size.height/sceneScaling;
    
    //character text
    NSString* flyText = @"This is you";
    SKLabelNode *flyLabel = [Constants createLabelWithFont:normalFont text:flyText fontColor:[SKColor grayColor] fontSize:fontSize];
    flyLabel.position = CGPointMake(characterXPosition*scaleCoef, self.frame.size.height/1.6);
    [self addChild:flyLabel];
    
    CharacterNode* flyCharacter = [CharacterNode characterWithPosition:CGPointMake(characterXPosition*scaleCoef, self.frame.size.height/2) inScene:self];
    [flyCharacter setScale: scaleCoef];
    [self addChild:flyCharacter];
    
    //joystick text
    NSString* joystickText = @"Move character";
    SKLabelNode* joystickLabel = [Constants createLabelWithFont:normalFont text:joystickText fontColor:[SKColor grayColor] fontSize:fontSize];
    joystickLabel.position = CGPointMake(self.scene.frame.origin.x+joystickOffset*scaleCoef*4, self.scene.frame.origin.y+joystickOffset*scaleCoef/.9);
    [self addChild:joystickLabel];
    
    SKSpriteNode* joystickPic = [SKSpriteNode spriteNodeWithImageNamed:@"joystick.png"];
    SKSpriteNode* padPic = [SKSpriteNode spriteNodeWithImageNamed:@"dpad.png"];
    joystickPic.position = CGPointMake(self.scene.frame.origin.x+joystickOffset*scaleCoef, self.scene.frame.origin.y+joystickOffset*scaleCoef/joystickHeightScale);
    padPic.position = CGPointMake(self.scene.frame.origin.x+joystickOffset*scaleCoef, self.scene.frame.origin.y+joystickOffset*scaleCoef/joystickHeightScale);
    [joystickPic setScale:scaleCoef];
    [padPic setScale:scaleCoef];
    [self addChild:joystickPic];
    [self addChild:padPic];
    
    //shooting text
    NSString* fireText = @"Fire";
    SKLabelNode* fireLabel = [Constants createLabelWithFont:normalFont text:fireText fontColor:[SKColor grayColor] fontSize:fontSize];
    fireLabel.position = CGPointMake(self.scene.size.width-fireButtonOffset*scaleCoef, fireButtonOffset*scaleCoef/textHeightScale);
    [self addChild:fireLabel];
    
    SKSpriteNode* firePic =  [SKSpriteNode spriteNodeWithImageNamed:@"FireButton.png"];
    firePic.position = CGPointMake(self.scene.size.width-fireButtonOffset*scaleCoef, fireButtonOffset*scaleCoef);
    [firePic setScale:scaleCoef/2];
    [self addChild:firePic];

    //shield text
    NSString* shieldText = @"Shield";
    SKLabelNode* shieldLabel = [Constants createLabelWithFont:normalFont text:shieldText fontColor:[SKColor grayColor] fontSize:fontSize];
    shieldLabel.position = CGPointMake(self.scene.size.width-fireButtonOffset*scaleCoef-fireLabel.frame.size.width-fireButtonOffset*scaleCoef, fireButtonOffset*scaleCoef/textHeightScale);
    [self addChild:shieldLabel];
    
    SKSpriteNode* shieldPic = [SKSpriteNode spriteNodeWithImageNamed:@"BlueShieldButton.png"];
    shieldPic.position = CGPointMake(self.scene.size.width-fireButtonOffset*scaleCoef-firePic.size.width,fireButtonOffset*scaleCoef);
    [shieldPic setScale:scaleCoef/2];
    [self addChild:shieldPic];
    
    //bonus text
    NSString* bonusText = @"Bonus point";
    SKLabelNode* bonusLabel = [Constants createLabelWithFont:normalFont text:bonusText fontColor:[SKColor grayColor] fontSize:fontSize];
    bonusLabel.position = CGPointMake(self.scene.size.width/picsScale-fireButtonOffset*scaleCoef, self.scene.size.height/bonusLabelHeightScale);
    [self addChild:bonusLabel];
    
    SKSpriteNode* bonusPic = [SKSpriteNode spriteNodeWithImageNamed:@"arc_reactor_circle"];
    bonusPic.position = CGPointMake(self.scene.size.width/picsScale-fireButtonOffset*scaleCoef, self.scene.size.height/bonusPicHeightScale);
    [bonusPic setScale:scaleCoef/2];
    [self addChild:bonusPic];
    
    //heal bonus text
    NSString* healText = @"Bonus life";
    SKLabelNode* healLabel = [Constants createLabelWithFont:normalFont text:healText fontColor:[SKColor grayColor] fontSize:fontSize];
    healLabel.position = CGPointMake(self.scene.size.width/picsScale-fireButtonOffset*scaleCoef, self.scene.size.height/healLabelHeightScale);
    [self addChild:healLabel];
    
    SKSpriteNode* healPic = [SKSpriteNode spriteNodeWithImageNamed:@"green-plus"];
    healPic.position = CGPointMake(self.scene.size.width/picsScale-fireButtonOffset*scaleCoef, self.scene.size.height/healPicHeightScale);
    [healPic setScale:scaleCoef/2];
    [self addChild: healPic];
    
    
    //triple laser bonus text
    NSString* tripleLaserText = @"Triple laser";
    SKLabelNode* tripleLaserLabel = [Constants createLabelWithFont:normalFont text:tripleLaserText fontColor:[SKColor grayColor] fontSize:fontSize];
    tripleLaserLabel.position = CGPointMake(self.scene.size.width/picsScale-fireButtonOffset*scaleCoef, self.scene.size.height/picsScale);
    [self addChild:tripleLaserLabel];
    
    SKSpriteNode* tripleLaserPic = [SKSpriteNode spriteNodeWithImageNamed:@"Linear_Divider"];
    tripleLaserPic.position = CGPointMake(self.scene.size.width/picsScale-fireButtonOffset*scaleCoef, self.scene.size.height/(textHeightScale*3));
    [tripleLaserPic setScale:scaleCoef];
    [self addChild: tripleLaserPic];
    
    //ship text
    NSString* shipText = @"Enemy Ship";
    SKLabelNode* shipLabel = [Constants createLabelWithFont:normalFont text:shipText fontColor:[SKColor grayColor] fontSize:fontSize];
    shipLabel.position = CGPointMake(self.scene.size.width-retreatRightBorder*scaleCoef, self.scene.size.height/(textHeightScale*3));
    [self addChild:shipLabel];
    
    SKSpriteNode* shipPic = [SKSpriteNode spriteNodeWithImageNamed:@"ship-fighter"];
    shipPic.position = CGPointMake(self.scene.size.width-retreatRightBorder*scaleCoef, self.scene.size.height/shipPicHeightScale);
    [shipPic setScale:scaleCoef/2];
    [self addChild:shipPic];
    
    //rocket text
    NSString* rocketText = @"Enemy Rocket";
    SKLabelNode* rocketLabel = [Constants createLabelWithFont:normalFont text:rocketText fontColor:[SKColor grayColor] fontSize:fontSize];
    rocketLabel.position = CGPointMake(self.scene.size.width-retreatRightBorder*scaleCoef, self.scene.size.height/healPicHeightScale);
    [self addChild:rocketLabel];
    
    SKSpriteNode* rocketPic = [SKSpriteNode spriteNodeWithImageNamed:@"red-rocket"];
    rocketPic.position = CGPointMake(self.scene.size.width-retreatRightBorder*scaleCoef, self.scene.size.height/rocketPicHeightScale);
    [rocketPic setScale:scaleCoef/2];
    [self addChild:rocketPic];
}

@end
